package com.example.myapplication.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.SearchActivity;
import com.example.myapplication.SongListActivity;
import com.example.myapplication.adapter.RankAdapter;
import com.example.myapplication.adapter.SongListAdapter;
import com.example.myapplication.api.MusicApi;
import com.example.myapplication.api.SongListApi;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.RankList;
import com.example.myapplication.entity.SongList;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HomeFragment extends Fragment {
    private Context context;
    private RecyclerView recyclerView;
    private SongListAdapter adapter;
    private List<SongList> songLists = new ArrayList<>();
    private RecyclerView recyclerViewRank;
    private RankAdapter rankAdapter;
    private List<RankList> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(container.getContext()).inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();

        Banner banner = view.findViewById(R.id.banner);
        List<String> img = new ArrayList<>();
        img.add("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2264494036,1851510776&fm=15&gp=0.jpg");
        img.add("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1445892336,3184816606&fm=15&gp=0.jpg");
        img.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1280512790,3179627704&fm=26&gp=0.jpg");

        banner.setAdapter(new BannerImageAdapter<String>(img) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                //图片加载自己实现
                Glide.with(holder.itemView)
                        .load(data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                        .into(holder.imageView);
            }
        })
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(context));

        recyclerView = view.findViewById(R.id.recycler_view);
        LinearLayoutManager manager = new LinearLayoutManager(context);

        manager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        adapter = new SongListAdapter(context);
        recyclerView.setAdapter(adapter);

        loadData();

        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(getActivity(), SongListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("songListID", songLists.get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


        //搜索
        TextView tvSearch = view.findViewById(R.id.tv_search);
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SearchActivity.class);
                startActivity(intent);
            }
        });

        recyclerViewRank = view.findViewById(R.id.recycler_view_rank);
        LinearLayoutManager manager1 = new LinearLayoutManager(context);
        manager1.setOrientation(RecyclerView.HORIZONTAL);
        recyclerViewRank.setLayoutManager(manager1);
        rankAdapter = new RankAdapter(context);
        recyclerViewRank.setAdapter(rankAdapter);
        getRank();

        rankAdapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(getActivity(), SongListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("songListID", list.get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    private void loadData(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        SongListApi api = retrofit.create(SongListApi.class);

        Call<BaseResponse> call = api.getSongList();
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                if (baseResponse.isSuccess()){
                    songLists = baseResponse.getPlaylists();
                    adapter.setData(songLists);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void getRank(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);

        Call<BaseResponse> call = api.getRank();
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                list = baseResponse.getList();
                rankAdapter.setData(list);
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}